package br.edu.ifrn.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.edu.ifrn.beans.Convenio;
import br.edu.ifrn.persistencia.ConvenioDAO;

@SuppressWarnings("serial")
public class EditarConvenio extends HttpServlet {
	HttpSession sessao;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sessao = req.getSession();
		editarConvenio(req, resp);
	}

	private void editarConvenio(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		ConvenioDAO dao = new ConvenioDAO();
		
		Convenio convenio = new Convenio();
		convenio.setCNPJ(req.getParameter("cnpj"));
		convenio.setNome(req.getParameter("nome"));
		convenio.setNomeFantasia(req.getParameter("nomeFantasia"));
		convenio.setLogradouro(req.getParameter("logradouro"));
		convenio.setNumero(req.getParameter("numero"));
		convenio.setBairro(req.getParameter("bairro"));
		convenio.setCep(req.getParameter("cep"));
		convenio.setCidade(req.getParameter("cidade"));
		convenio.setUF(req.getParameter("uf"));
		convenio.setComplemento(req.getParameter("complemento"));
		convenio.setTelefone(req.getParameter("telefone"));
		convenio.setEmail(req.getParameter("email"));
		convenio.setRepresentante(req.getParameter("representante"));
		convenio.setCpfRepresentante(req.getParameter("cpfRepresentante"));
		
		dao.editarConvenio(convenio);
		sessao.setAttribute("convenio", convenio);

		String mensagem = "Dados alterados com sucesso";
		sessao.setAttribute("mensagem", mensagem);
		RequestDispatcher jsp = req.getRequestDispatcher("convenios.jsp");
		jsp.forward(req, resp);

	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sessao = req.getSession();
		sessao.removeAttribute("erro1");
		editarsenha(req, resp);
	}
	
	private void editarsenha(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		ConvenioDAO dao = new ConvenioDAO();
		Convenio convenioApp = (Convenio) sessao.getAttribute("convenio");
		
		Convenio convenioBanco = dao.recuperarPorCNPJ(convenioApp.getCNPJ());
		
		if (convenioBanco.getSenha().equals(req.getParameter("antiga"))){
			if (req.getParameter("nova1").equals(req.getParameter("nova2"))) {
				
				convenioBanco.setSenha(req.getParameter("nova1"));
				dao.editarSenha(convenioBanco);
				sessao.setAttribute("convenio", convenioBanco);
				String mensagem = "Senha alterada com sucesso";
				sessao.setAttribute("mensagem", mensagem);
				RequestDispatcher jsp = req.getRequestDispatcher("convenios.jsp");
				jsp.forward(req, resp);
			}else{
				String erro1 = "Novas senhas digitadas diferentes";
				sessao.setAttribute("erro1", erro1);
				RequestDispatcher jsp = req.getRequestDispatcher("editarsenhaconvenio.jsp");
				jsp.forward(req, resp);
			}
				
		}else{
			String erro1 = "Senha incorreta";
			sessao.setAttribute("erro1", erro1);
			RequestDispatcher jsp = req.getRequestDispatcher("editarsenhaconvenio.jsp");
			jsp.forward(req, resp);
		}
	}

}
